<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Code by TinhNQ
 * 2014
 **/
class Restaurant extends Admin_Controller {

    function __construct(){
        parent::__construct();
        $this->admin_navigation->parent_active('restaurant');
        $this->load->library('session');

        //$this->output->enable_profiler(TRUE);
        $this->load->model('restaurant/restaurant_model');
        $this->load->model('pref/regions_model');
        $this->load->model('pref/prefs_model');
        $this->load->model('pref/citys_model');
        $this->load->model('restaurant/cuisines_model');
    }

    function manager_restaurant() {
        $columns = array(
                array(
                    'name' => 'ID',
                    'type' => 'id',
                    'index' => 'restaurant_id',
                    'filter' => 'restaurant_id',
                    'sort_column' => true,
                ),
                array(
                    'name' => 'name',
                    'type' => 'text',
                    'index' => 'name',
                    'filter' => 'name',
                    'sort_column' => true,
                ),
                array(
                    'name' => 'Tỉnh thành',
                    'type' => 'select',
                    'index' => 'pref_name',
                    'filter' => 'pref_id',
                    'options' => $this->prefs_model->get_prefs_options(),
                ),
                array(
                    'name' => 'is_hot',
                    'type' => 'publish',
                    'index' => 'is_hot',
                    'filter' => 'is_hot',
                    'sort_column' => true,
                    'options' => array('0' => 'Không', '1' => 'Có'),
                ),
                array(
                    'name' => 'featured',
                    'type' => 'publish',
                    'index' => 'featured',
                    'filter' => 'featured',
                    'sort_column' => true,
                    'options' => array('0' => 'Không', '1' => 'Có'),
                ),
                array(
                    'name' => 'created',
                    'type' => 'date',
                    'index' => 'created',
                    'sort_column' => true,
                ),
                array(
                    'name' => 'status',
                    'type' => 'publish',
                    'index' => 'status',
                    'filter' => 'status',
                    'sort_column' => true,
                    'options' => array('0' => 'Không', '1' => 'Có'),
                ),
            );
        $this->grid->columns($columns);
        $this->grid->datasource('restaurant/restaurant_model','get_restaurants');
        $this->grid->set_title('Quản lý quán ăn');
        $this->grid->add_button('Thêm mới',admin_url('*/restaurant/add_restaurant'),'add');
        $this->grid->set_row_action('edit_restaurant');
        $this->grid->total_rows($this->restaurant_model->get_count());
        $this->grid->add_mass_action('delete', array(
                'label'=>'Xóa',
                'url'      => admin_url('*/restaurant/delete_restaurant'),
                'confirm'  => 'Are you sure?'
        ));
        $this->grid->add_mass_action('status', array(
                'label'=>'Thay đổi trạng thái',
                'url'      => admin_url('*/restaurant/change_status_restaurant'),
        ));
        if ($this->input->is_ajax()){
            $this->output->set_output($this->grid->display());
        }else{
            $this->load->view('admin/grid');
        }
    }

    function edit_restaurant() {
        $restaurant_id = $this->input->get_param('id', false);
        if ($restaurant_id) {
            $data = $this->restaurant_model->get_restaurant($restaurant_id);
            $data['cuisine_id[]'] = $this->restaurant_model->get_cuisine_restaurant($restaurant_id);
            $this->form->set_values($data);
        }


        $this->form->set_form_attributes(array(
            'id'=>'restaurant_form',
            'action'=>admin_url('*/restaurant/edit_post_restaurant',array('id'=>$restaurant_id)),
            'enctype'=>'multipart/form-data',
            'onsubmit'=> "selectAll('right_cuisine_id[]')",
        ));

        $this->form->add_filed('name', 'text', array(
            'label' => 'Tên Quán',
            'name' => 'name',
            'required' => true,
            ));

        $this->form->add_filed('alias', 'text', array(
            'label' => 'alias',
            'name' => 'alias',
            ));
        $this->form->add_filed('cuisine_id', 'select-multi', array(
            'label' => 'Ẩm thực',
            'values'=> $this->cuisines_model->get_cuisines_options(),
            'name' => 'cuisine_id[]',
            ));

        $this->form->add_filed('opening', 'text', array(
            'label' => 'Thời gian bán',
            'name' => 'opening',
            'required' => true,
            ));
        $this->form->add_filed('image1', 'image', array(
            'label' => 'Hình ảnh 1',
            'name' => 'image1',
            ));
        $this->form->add_filed('image2', 'image', array(
            'label' => 'Hình ảnh 2',
             'name' => 'image2', ));
        $this->form->add_filed('image3', 'image', array(
            'label' => 'Hình ảnh 3',
            'name' => 'image3', ));
        $this->form->add_filed('image4', 'image', array(
            'label' => 'Hình ảnh 4',
            'name' => 'image4', ));
        $this->form->add_filed('image5', 'image', array(
            'label' => 'Hình ảnh 5',
            'name' => 'image5', ));
        $this->form->add_filed('region_id', 'select', array(
            'label' => 'Khu vực',
            'name' => 'region_id',
            'onchange' => 'changeRegions(this)',
            'values'=> $this->regions_model->get_regions_options(),
            'required' => true,));
        $this->form->add_filed('pref_id', 'select', array(
            'label' => 'Tỉnh thành',
            'onchange' => 'changePrefs(this)',
            'values'=> $this->prefs_model->get_prefs_options(),
            'name' => 'pref_id',
            'required' => true,));
        $this->form->add_filed('city_id', 'select', array(
            'label' => 'Quận huyện',
            'values'=> $this->citys_model->get_citys_options(),
            'name' => 'city_id',
            'required' => true,));
        $this->form->add_filed('address', 'text', array(
            'label' => 'Địa chỉ',
            'name' => 'address', ));
        $this->form->add_filed('localtion', 'text', array(
            'label' => 'localtion',
            'name' => 'localtion', ));
        $this->form->add_filed('tel', 'text', array(
            'label' => 'Số điện thoại',
            'name' => 'tel', ));
        $this->form->add_filed('url', 'text', array(
            'label' => 'website',
            'name' => 'url', ));
        $this->form->add_filed('is_hot', 'select', array(
            'label' => 'Hot',
            'name' => 'is_hot',
            'required' => true,
            'values'=>array('Tắt','Bật')));
        $this->form->add_filed('featured', 'select', array(
            'label' => 'Nổi bật',
            'name' => 'featured',
            'required' => true,
            'values'=>array('Tắt','Bật')));
        $this->form->add_filed('status', 'select', array(
            'label' => 'Trạng thái',
            'name' => 'status',
            'values'=>array('Tắt','Bật')));

        $this->form->add_filed('introtext', 'textarea', array(
            'label' => 'Giới thiệu sơ lược',
            'name' => 'introtext',
            'required' => true,));
        $this->form->add_filed('fulltext', 'editor', array(
            'label' => 'Bài viết',
            'name' => 'fulltext',
            ));


        $this->load->view('admin/form', array('form' => $this->form->to_html()));
    }

    function add_restaurant() {
        $this->_forward('edit_restaurant');
    }

    function delete_restaurant() {
        $cids = explode(',', $this->input->get_param('restaurant_id',false));
        $this->load->model('restaurant/restaurant_model');
        foreach ($cids as $cid) {
            $this->restaurant_model->delete_restaurant($cid);
        }
        $this->_redirect(admin_url('*/restaurant/manager_restaurant'));
    }

    function change_status_restaurant() {
        $cids = explode(',', $this->input->get_param('restaurant_id',false));
        $this->load->model('restaurant/restaurant_model');
        foreach ($cids as $cid) {
            $this->restaurant_model->change_status_restaurant($cid);
        }
        $this->_redirect(admin_url('*/restaurant/manager_restaurant'));
    }

    function edit_post_restaurant() {
        $restaurant_id = $this->input->get_param('id', false);
        $this->load->model('restaurant/restaurant_model');
        if (($data = $this->input->post())) {
            $config['upload_path'] = './images/content/';
            $config['allowed_types'] = 'gif|jpg|png';
            $this->load->library('upload', $config);
            if ($this->upload->do_upload('image1')){
                $data['image1'] = 'images/content/'.$this->upload->get_file_name();
            }
            if ($this->upload->do_upload('image2')){
                $data['image2'] = 'images/content/'.$this->upload->get_file_name();
            }
            if ($this->upload->do_upload('image3')){
                $data['image3'] = 'images/content/'.$this->upload->get_file_name();
            }
            if ($this->upload->do_upload('image4')){
                $data['image4'] = 'images/content/'.$this->upload->get_file_name();
            }
            if ($this->upload->do_upload('image5')){
                $data['image5'] = 'images/content/'.$this->upload->get_file_name();
            }

            if ($restaurant_id) {
                $this->restaurant_model->update_restaurant($data, $restaurant_id);
            } else {
                $this->restaurant_model->insert_restaurant($data);
            }

            $this->_redirect(admin_url('*/restaurant/manager_restaurant'));
        }
    }

    function ajax_prefs_by_region_id() {
        $data = $this->input->get();
        $filters = array('region_id' => $data['region_id']);
        $resturn = $this->prefs_model->get_prefs_options($filters);

        print $this->get_options_json($resturn);
        exit;
    }

    function ajax_prefs_by_pref_id() {
        $data = $this->input->get();
        $filters = array('pref_id' => $data['pref_id']);
        $resturn = $this->citys_model->get_citys_options($filters);

        print $this->get_options_json($resturn);
    }

    function get_options_json($resturn)
    {
        $options_str = "[";
        $tmp_array = array();
        foreach ($resturn as $key => $value) {
            array_push($tmp_array,"['".$key."','".$value."']");
        }
        $options_str .= join(',',$tmp_array);
        $options_str .= "]";

        return $options_str;
    }
}
?>